package com.mccloud.mini.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mccloud.mini.entity.YbGoods;
import com.mccloud.mini.mapper.YbGoodsMapper;
import com.mccloud.mini.service.IYbGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mccloud.mini.util.DouDianUtil;
import com.mccloud.mini.vo.AddGoodsReq;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.TreeMap;

/**
 * <p>
 * 商品 服务实现类
 * </p>
 *
 * @author 宋发准
 * @since 2020-11-12
 */
@Service
public class YbGoodsServiceImpl extends ServiceImpl<YbGoodsMapper, YbGoods> implements IYbGoodsService {
    /**
     * 执行抖店方法
     *
     * @param url
     * @param method
     * @param paramJson
     * @return
     */
    public static String executeRequest(String url, String method, String paramJson) {
        String now = DateUtil.now();
        String v = "2";
        String sign = DouDianUtil.getSign("6873668517658314254", "8a988518-d400-4c7d-bafb-9c503cef281b", method, paramJson, now, v);
        StrBuilder sb = new StrBuilder();
        sb.append(url)
                .append("?app_key=").append("6873668517658314254")
                .append("&method=").append(method)
                .append("&access_token=").append("41d30b7b-2279-4430-8c46-b5d77d80b20e")
                .append("&param_json=").append(paramJson)
                .append("&timestamp=").append(now)
                .append("&v=").append(v)
                .append("&sign=").append(sign);
        return HttpUtil.get(sb.toString());
    }

    /**
     * 添加商品
     *
     * @param req
     * @return
     */
    @Override
    public Boolean addGoods(AddGoodsReq req) {
        String spec = getSpec("108894510");

        YbGoods goods = new YbGoods();
        goods.setOutProductId(System.currentTimeMillis() + "");
        goods.setName(req.getName());
        goods.setPic("http://140.143.225.155:8099/1.jpg|http://140.143.225.155:8099/2.jpg");
        goods.setDescription("http://140.143.225.155:8099/xq.gif");
        goods.setMarketPrice(req.getMarketPrice());
        goods.setDiscountPrice(req.getDiscountPrice());
        goods.setFirstCid("5");
        goods.setSecondCid("48");
        goods.setThirdCid("413");
        goods.setSpecId("108894510");
        goods.setSpecPic("934635266|http://140.143.225.155:8099/1_1.jpg^934635267|http://140.143.225.155:8099/1_2.jpg^934635268|http://140.143.225.155:8099/1_3.jpg");
        goods.setMobile("18510417516");
        goods.setWeight("100");
        goods.setProductFormat("材质|羊毛^款式|开衫");
        goods.setPayType("2");

        String url = "https://openapi-fxg.jinritemai.com/product/add";
        String method = "product.add";
        TreeMap<String, String> param = new TreeMap<>();
        param.put("out_product_id", goods.getOutProductId());
        param.put("name", goods.getName());
        param.put("pic", goods.getPic());
        param.put("description", goods.getDescription());
        param.put("market_price", goods.getMarketPrice());
        param.put("discount_price", goods.getDiscountPrice());
        param.put("first_cid", "5");
        param.put("second_cid", "48");
        param.put("third_cid", "413");
        param.put("spec_id", goods.getSpecId());
        param.put("spec_pic", goods.getSpecPic());
        param.put("mobile", goods.getMobile());
        param.put("weight", goods.getWeight());
        param.put("pay_type", "2");
        param.put("product_format", goods.getProductFormat());
        String paramJson = DouDianUtil.getParamJson(param);
        String str = executeRequest(url, method, paramJson);
        System.out.println("str = " + str);
        JSONObject jsonObject = JSONUtil.parseObj(str);
        Integer err_no = jsonObject.getInt("err_no");
        if (err_no == 0) {
            JSONObject data1 = jsonObject.getJSONObject("data");
            String product_id = data1.getLong("product_id") + "";
            //添加sku
            JSONObject specJson = JSONUtil.parseObj(spec);
            Integer spec_no = specJson.getInt("err_no");
            if (spec_no == 0) {
                JSONObject data = specJson.getJSONObject("data");
                JSONArray specs = data.getJSONArray("Specs");
                ArrayList<Integer> one = new ArrayList<>();
                ArrayList<Integer> two = new ArrayList<>();

                for (int i = 0; i < specs.size(); i++) {
                    JSONObject SpecsJson = specs.getJSONObject(i);
                    JSONArray values = SpecsJson.getJSONArray("values");
                    for (int j = 0; j < values.size(); j++) {
                        JSONObject jsonObject1 = values.getJSONObject(j);
                        Integer id1 = jsonObject1.getInt("id");
                        if(i==0){
                            one.add(id1);
                        }else {
                            two.add(id1);
                        }
                    }
                }
                for (Integer integer : one) {
                    for (Integer integer1 : two) {
                        String spec_detail_ids = integer + "|" + integer1;
                        addSku(product_id, goods.getSpecId(), spec_detail_ids);
                    }
                }
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    private String addSku(String product_id, String specId, String spec_detail_ids) {
        String url = "https://openapi-fxg.jinritemai.com/sku/add";
        String method = "sku.add";
        TreeMap<String, String> param = new TreeMap<>();
        param.put("product_id", product_id);
        param.put("out_sku_id", System.currentTimeMillis() + "");
        param.put("spec_id", specId);
        param.put("spec_detail_ids", spec_detail_ids);
        param.put("stock_num", "100");
        param.put("price", "100");
        String paramJson = DouDianUtil.getParamJson(param);
        return executeRequest(url, method, paramJson);
    }

    private static String getSpec(String spec) {
        String url = "https://openapi-fxg.jinritemai.com/spec/specDetail";
        String method = "spec.specDetail";
        TreeMap<String, String> param = new TreeMap<>();
        param.put("id", spec);
        String paramJson = DouDianUtil.getParamJson(param);
        return executeRequest(url, method, paramJson);
    }


    public static void main(String[] args) {
        String url="https://openapi-fxg.jinritemai.com/product/list";
        String method="product.list";
        TreeMap<String, String> param = new TreeMap<>();
        param.put("page", "0");
        param.put("size", "100");
        String paramJson = DouDianUtil.getParamJson(param);
        String str = executeRequest(url,method,paramJson);
        System.out.println("str = " + str);
    }

}
